package com.example.appmanager.user;
import android.os.Bundle;
import android.util.Log;

import com.example.appmanager.Appconfig.Appconfig;
import com.example.appmanager.R;
import com.example.appmanager.adapter.AppAdapter;
import com.example.appmanager.entity.AppEntity;
import com.example.appmanager.services.AllresposeService;

import java.util.ArrayList;
import java.util.List;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/*用户根据类型查看APP*/
public class AppTypeShow extends AppCompatActivity {
    private RecyclerView recyclerView;
    private AppAdapter adapter ;
    private List<AppEntity.RowsBean> rowsBeans = new ArrayList<>();
    private AppEntity.RowsBean bean = new AppEntity.RowsBean();
    String type = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_type_show);
        initUi();
        getData(type);//请求数据
    }
    private void initUi() {
        recyclerView = findViewById(R.id.app_type_show);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AppAdapter(getApplicationContext(),null);
        // bundle.putString("type","教育");

        type = (String) getIntent().getExtras().get("type");
        Log.d("AppTypeShow",type);
    }
    private void getData(String type) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(Appconfig.BASE_URL)
                .client(Appconfig.CLIENT) //这是设置请求的时间等信息 连接时间  上传等时间
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        AllresposeService request = retrofit.create(AllresposeService.class);
        Call<AppEntity> appCall = request.queryAppByType(type);
        appCall.enqueue(new Callback<AppEntity>() {
            @Override
            public void onResponse(Call<AppEntity> call, Response<AppEntity> response) {
                assert response.body() != null;
                rowsBeans = response.body().getRows();
                Log.d("AppTypeShow", rowsBeans.toString());
                adapter.setData(getApplicationContext(), rowsBeans);
                adapter.notifyDataSetChanged();
                recyclerView.setAdapter(adapter);
                Log.d("AppTypeShow", rowsBeans.toString());
            }
            @Override
            public void onFailure(Call<AppEntity> call, Throwable t) {
               // recyclerView.setAdapter(adapter);
                Log.d("AppTypeShow", "请求错误" + t.getMessage());
            }
        });
    }
}
